package com.android.toolbar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Toolbar mToolBar2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initView2();
        initView3();
    }



    private void initView() {
        mToolBar2 = (Toolbar) findViewById(R.id.tool_bar_2);

        //点击了NaVigationButton
        mToolBar2.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击了NavigationButton",Toast.LENGTH_LONG).show();
            }
        });
        // 设置 toolbar 背景色
        mToolBar2.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

        // 设置 Title
        mToolBar2.setTitle(R.string.toolbar_title);
        mToolBar2.setTitleTextColor(getResources().getColor(R.color.white));

        // 设置Toolbar subTitle
        mToolBar2.setSubtitle(R.string.sub_title);
        mToolBar2.setSubtitleTextColor(getResources().getColor(R.color.white));

        // 设置logo
        mToolBar2.setLogo(R.mipmap.ic_launcher);

        //设置NavigationIcon
        mToolBar2.setNavigationIcon(R.drawable.ic_book_list);
        mToolBar2.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //设置菜单menu
        mToolBar2.inflateMenu(R.menu.setting_menu);
        // 设置menu item 点击事件
        mToolBar2.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.item_setting:
                        //点击设置
                        break;
                }
                return false;
            }
        });
    }

    private void initView2() {
        Toolbar mToolBar1 = (Toolbar) findViewById(R.id.toolBar1);
        mToolBar1.inflateMenu(R.menu.setting_menu);
    }
    private void initView3() {
        Toolbar mToolbar3 = (Toolbar) findViewById(R.id.tool_bar_3);
        mToolbar3.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mToolbar3.inflateMenu(R.menu.menu_search);

        mToolbar3.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if(item.getItemId() == R.id.item_search){
                    // do search
                }
                return false;
            }
        });
        // 获取ToolBar 上的编辑框
        EditText searchEdit = (EditText) mToolbar3.findViewById(R.id.edit_search);
        // 获取内容
        String content = searchEdit.getText().toString();

    }

}
